package cn.parus.dynamicRouter.service;

import cn.parus.dynamicRouter.utils.JwtUtils;
import cn.parus.commons.common.Execption.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: JwtService
 * @Author 潘春兰
 * @Package cn.parus.auth.service
 * @Date 2025/3/25 9:56
 * @description: TODO 对jwt进行判断是否有效
 * @Version:
 */
@Service
public class JwtService {

    @Autowired
    JwtUtils jwtUtils;

    /**
     * 使用jwt生成Token
     * @param jwtContent
     * @return
     */
    public Map<String, Object> createJwt(Map<String, Object> jwtContent) {
        if (jwtContent == null|| jwtContent.isEmpty()) {
            throw new CustomException("token内容不能为空");
        }
        // 生成jwt
        String jwt = jwtUtils.createJWT(jwtContent);
        Map<String, Object> token = new HashMap<>();
        token.put("token", jwt);
        return token;
    }

    /**
     * 使用JWT解密token
     * @param token
     * @return
     */

    public Map<String, Object> parseJwt(String token) {

        if(!jwtUtils.isNotExpired(token)){
            throw new CustomException("token已过期");
        }
        Map<String, Object> tokenContent = jwtUtils.parseJWT(token);
        return tokenContent;
    }
}
